/**
 * Copyright &copy; 2012-2016 <a href="https://github.com/thinkgem/jeesite">JeeSite</a> All rights reserved.
 */
package com.thinkgem.jeesite.modules.ta.web;

import java.util.Date;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.thinkgem.jeesite.common.config.Global;
import com.thinkgem.jeesite.common.persistence.Page;
import com.thinkgem.jeesite.common.web.BaseController;
import com.thinkgem.jeesite.common.utils.StringUtils;
import com.thinkgem.jeesite.modules.sys.utils.UserUtils;
import com.thinkgem.jeesite.modules.ta.entity.StSuspend;
import com.thinkgem.jeesite.modules.ta.entity.XtSuspendApply;
import com.thinkgem.jeesite.modules.ta.service.StSuspendService;
import com.thinkgem.jeesite.modules.ta.service.XtSuspendApplyService;

/**
 * 业务挂起Controller
 * 
 * @author TTAO
 * @version 2017-06-11
 */
@Controller
@RequestMapping(value = "${adminPath}/ta/stSuspend")
public class StSuspendController extends BaseController {

	@Autowired
	private StSuspendService stSuspendService;

	@Autowired
	private XtSuspendApplyService xtSuspendApplyService;

	@ModelAttribute
	public StSuspend get(@RequestParam(required = false) String id) {
		StSuspend entity = null;
		if (StringUtils.isNotBlank(id)) {
			entity = stSuspendService.get(id);
		}
		if (entity == null) {
			entity = new StSuspend();
		}
		return entity;
	}

	@RequiresPermissions("ta:stSuspend:view")
	@RequestMapping(value = { "list", "" })
	public String list(StSuspend stSuspend, HttpServletRequest request,
			HttpServletResponse response, Model model) {
		Page<StSuspend> page = stSuspendService.findPage(new Page<StSuspend>(
				request, response), stSuspend);
		model.addAttribute("page", page);
		return "modules/ta/stSuspendList";
	}

	@RequiresPermissions("ta:stSuspend:view")
	@RequestMapping(value = "form")
	public String form(StSuspend stSuspend, Model model) {
		model.addAttribute("stSuspend", stSuspend);
		return "modules/ta/stSuspendForm";
	}

	@RequiresPermissions("ta:stSuspend:edit")
	@RequestMapping(value = "save")
	public String save(StSuspend stSuspend, Model model,
			RedirectAttributes redirectAttributes) {
		if (!beanValidator(model, stSuspend)) {
			return form(stSuspend, model);
		}
		stSuspend.setEndDate(new Date());
		stSuspend.setEndUserid(UserUtils.getUser().getNo());
		stSuspendService.save(stSuspend);
		addMessage(redirectAttributes, "解除业务挂起成功");
		return "redirect:" + Global.getAdminPath() + "/ta/stSuspend/?repage";
	}

	@RequestMapping(value = "saveStSuspend")
	public String saveStSuspend(StSuspend stSuspend, String iid, Model model,
			RedirectAttributes redirectAttributes) {
		XtSuspendApply xtSuspendApply = xtSuspendApplyService
				.getXtSuspendApply(iid);
		stSuspend.setIid(xtSuspendApply.getSerial());
		stSuspend.setStartDate(new Date());
		stSuspend.setUserid(UserUtils.getUser().getNo());
		stSuspend.setDescription(xtSuspendApply.getMemo());
		stSuspend.setSuspendType(xtSuspendApply.getSuspendType());
		stSuspend.setApplyId(xtSuspendApply.getId());
		stSuspend.setEndUserid(null);
		stSuspend.setEndDate(null);
		stSuspendService.save(stSuspend);
		xtSuspendApply.setIsdelete(Long.parseLong("0"));
		xtSuspendApply.setDeleteTime(new Date());
		xtSuspendApplyService.save(xtSuspendApply);
		addMessage(redirectAttributes, "保存业务挂起成功");
		return "redirect:" + Global.getAdminPath() + "/ta/stSuspend/?repage";
	}

	// @RequestMapping(value = "saveStSuspendByApply")
	// public String saveStSuspendByApply(StSuspend stSuspend, String iid,
	// Model model, RedirectAttributes redirectAttributes) {
	// XtSuspendApply xtSuspendApply = xtSuspendApplyService
	// .getXtSuspendApply(iid);
	// stSuspend.setIid(xtSuspendApply.getSerial());
	// stSuspend.setStartDate(new Date());
	// stSuspend.setUserid(UserUtils.getUser().getNo());
	// stSuspend.setDescription(xtSuspendApply.getMemo());
	// stSuspend.setSuspendType(xtSuspendApply.getSuspendType());
	// stSuspend.setEndUserid(null);
	// stSuspend.setEndDate(null);
	// stSuspend.setApplyId(null);
	// stSuspendService.save(stSuspend);
	// return "redirect:" + Global.getAdminPath() + "/ta/xtSuspendApply/list";
	// }

	@RequiresPermissions("ta:stSuspend:edit")
	@RequestMapping(value = "delete")
	public String delete(StSuspend stSuspend,
			RedirectAttributes redirectAttributes) {
		stSuspendService.delete(stSuspend);
		addMessage(redirectAttributes, "删除业务挂起成功");
		return "redirect:" + Global.getAdminPath() + "/ta/stSuspend/?repage";
	}

}